package org.example.managementsystem.controller;

import org.example.managementsystem.model.Person;
import org.example.managementsystem.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员信息控制器
 */
@RestController
@RequestMapping("/api/persons")
@CrossOrigin(origins = "*")
public class PersonController {
    
    private final PersonService personService;

    @Autowired
    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    /**
     * 获取所有人员信息
     */
    @GetMapping
    public ResponseEntity<List<Person>> getAllPersons() {
        List<Person> persons = personService.getAllPersons();
        return ResponseEntity.ok(persons);
    }

    /**
     * 根据ID获取人员信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Person> getPersonById(@PathVariable Long id) {
        Person person = personService.getPersonById(id);
        if (person != null) {
            return ResponseEntity.ok(person);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 添加人员
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addPerson(@RequestBody Person person) {
        Person savedPerson = personService.addPerson(person);
        Map<String, Object> response = new HashMap<>();
        response.put("id", savedPerson.getId());
        response.put("message", "人员添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新人员信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, String>> updatePerson(@PathVariable Long id, @RequestBody Person person) {
        person.setId(id);
        boolean updated = personService.updatePerson(person);
        
        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "人员信息更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "人员信息更新失败，未找到指定ID的人员");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 删除人员
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deletePerson(@PathVariable Long id) {
        boolean deleted = personService.deletePerson(id);
        
        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "人员删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "人员删除失败，未找到指定ID的人员");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 搜索人员
     */
    @GetMapping("/search")
    public ResponseEntity<List<Person>> searchPersons(@RequestParam String keyword) {
        List<Person> persons = personService.searchPersons(keyword);
        return ResponseEntity.ok(persons);
    }
}
